package com.jic.member.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.member.entity.MememberGrowthLog;
import com.jic.member.entity.MememberUser;
import com.jic.member.mapper.MememberGrowthLogMapper;
import com.jic.member.mapper.MememberUserMapper;
import com.jic.member.request.MemberGrowthLogRequest;
import com.jic.member.response.MemberGrowthLogResponse;
import com.jic.member.service.MememberGrowthLogService;
import com.jic.common.base.vo.PageResult;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 成长值流水表
 * @author : 
 * @email : 
 * @since : 2020-04-15 07:30:56
 * @version : v1.0.0
 */
@Slf4j
@Service
public class MememberGrowthLogServiceImpl implements MememberGrowthLogService{

    @Resource
    private MememberGrowthLogMapper mememberGrowthLogMapper;
    @Resource
    private MememberUserMapper mememberUserMapper;
    @Autowired
    private SysDeptApi sysDeptApi;

    @Override
    public RestResult<Long> insert(MemberGrowthLogRequest param) {
        RestResult result = new RestResult();
        MememberGrowthLog mememberGrowthLog = new MememberGrowthLog();
        BeanUtils.copyProperties(param,mememberGrowthLog);
        mememberGrowthLog.setCreator(param.getAdminOperate().getOperator());
        mememberGrowthLog.setCreateTime(new Date());
        mememberGrowthLog.setCreatorName(param.getAdminOperate().getOperateName());
        long flag = mememberGrowthLogMapper.insert(mememberGrowthLog);
         result.setData(flag);
        return result;
    }

    @Override
    public long delete(Long id) {
        return mememberGrowthLogMapper.delete(id);
    }

    @Override
    public long update(MememberGrowthLog mememberGrowthLog) {
        return mememberGrowthLogMapper.update(mememberGrowthLog);
    }

    @Override
    public MememberGrowthLog queryByPrimaryKey(Long id){
        return mememberGrowthLogMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberGrowthLog> query(MememberGrowthLog mememberGrowthLog){
        return mememberGrowthLogMapper.query(mememberGrowthLog);
    }

    @Override
    public RestResult<PageResult<MemberGrowthLogResponse>> queryPage(MemberGrowthLogRequest mememberGrowthLog) {
        RestResult result = new RestResult();

        mememberGrowthLog.setDeptId(mememberGrowthLog.getAdminOperate().getDeptId());
        //计算下标
        int startIndex = (mememberGrowthLog.getStart() - 1) * mememberGrowthLog.getLimit();
        List<MemberGrowthLogResponse> list = mememberGrowthLogMapper.queryPage(startIndex, mememberGrowthLog.getLimit(), mememberGrowthLog);
        long count = mememberGrowthLogMapper.queryPageCount(mememberGrowthLog);
        list.forEach( items->{
            if(items.getDeptType() == 2 || items.getDeptType() == 3){
                RestResult<String> msName = sysDeptApi.queryDeptNameById(items.getDeptId()); //所属商家或店铺名称
                RestResult<String> deptName = sysDeptApi.queryDeptNameById(items.getDeptId()); //所属子平台
                items.setMsName(msName.getData());
                items.setDeptName(deptName.getData());

            }
        });
        list.forEach( items->{
            if(items.getDeptType() == 1 ){
                RestResult<String> deptName = sysDeptApi.queryDeptNameById(items.getDeptId()); //所属子平台
                items.setDeptName(deptName.getData());
            }
        });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        result.setCode("0000");
        result.setData(pageResult);
        return result;
    }

    /**
     * 查询会员成长值流水列表
     * @param memberGrowthLogRequest
     * @return
     */
    @Override
    public PageResult<MemberGrowthLogResponse> queryMememberGrowthLog(MemberGrowthLogRequest memberGrowthLogRequest) {
        log.info("开始查询会员成长值流水列表==》总入参,{}",memberGrowthLogRequest);
        PageResult pageResult = new PageResult();
        int startIndex = (memberGrowthLogRequest.getStart() - 1) * memberGrowthLogRequest.getLimit();
        MememberUser mmMememberUser = new MememberUser();
        if (null == memberGrowthLogRequest){
            //根据第三方用户id查询 会员信息表
            mmMememberUser.setId(memberGrowthLogRequest.getMemberId());
            log.info("查询会员成长值流水列表==》查询会员信息==》入参,{}",mmMememberUser);
            List<MememberUser> resultUser = mememberUserMapper.query(mmMememberUser);
            log.info("查询会员成长值流水列表==》查询会员信息==》结果,{}",resultUser);
            if(CollectionUtils.isNotEmpty(resultUser)){
                memberGrowthLogRequest.setId(resultUser.get(0).getId().intValue());
                log.info("查询会员成长值流水列表==》入参,{}",memberGrowthLogRequest);
                List<MemberGrowthLogResponse> list = mememberGrowthLogMapper.queryPage(startIndex, memberGrowthLogRequest.getLimit(), memberGrowthLogRequest);
                log.info("查询会员成长值流水列表==》结果,{}",list);
                log.info("查询会员成长值流水列表总数count==》入参,{}",memberGrowthLogRequest);
                long count = mememberGrowthLogMapper.queryPageCount(memberGrowthLogRequest);
                log.info("查询会员成长值流水列表总数count==》结果,{}",count);
                pageResult.setRows(list);
                pageResult.setTotal(count);
            }
        }
        log.info("结束查询会员成长值流水列表==》返回值,{}", JSON.toJSONString(pageResult));
        return pageResult;
    }
}